var app = getApp();
Page({
	data: {
		orderList: [],
		type: '', //订单状态0全部 1代付款 2待收货 3待自提 4已完成 -1已取消
		pages: 0,
		page: 1
	},
	onLoad(options) {
		var that = this;
		if (options) {
			if (options.type) {
				var type = options.type;
				that.setData({
					type: type
				})
			}
		}
		that.loadlist()
	},
	onShow(){
		this.loadlist()
	},
	type_select: function(e) {
		var that = this;
		var i = e.currentTarget.dataset.index;
		that.setData({
			type: i
		})
		that.loadlist()
	},
	onReachBottom: function() {
		// 下拉触底，先判断是否有请求正在进行中
		// 以及检查当前请求页数是不是小于数据总页数，如符合条件，则发送请求
		if (!this.loading && this.data.page < this.data.pages) {
			this.data.page += 1
			this.fetchArticleList(this.data.page)
		}
	},
	fetchArticleList(pageNo) {
		var $th = this;
		var post = {
			status: this.data.type,
			page: pageNo,
			limit: 50
		}
		app.globalAjax(
			'/api/order/list',
			"get",
			post,
			function(e) {
				if (e.code == 0) {
					$th.setData({
						orderList: $th.data.orderList.concat(e.orderList.list)
					})
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			})
	},
	toOrderDetail: function(e){
		var i = e.target.dataset.index;
		var orderId = this.data.orderList[i].orderId;
		wx.navigateTo({
			url: '/pages/order/orderart/orderart?id=' + orderId
		})
	},
	cancleOrder: function(e){
		var i = e.target.dataset.index;
		var orderId = this.data.orderList[i].orderId;
		let _this = this;
		app.globalAjax(
			'/api/order/cancel',
			"get",
			{orderId:orderId},
			function(e) {
				if (e.code == 0) {
					wx.showToast({
						title: '删除成功',
						icon: "none"
					});
					_this.loadlist()
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			}, {
				token: wx.getStorageSync("token")
			})
	},
	loadlist: function() {
		var that = this;
		var t = that.data.type;
		if (t == 0) t = '';
		var data = {
			status: t,
			page: this.data.page,
			limit: 10
		}
		app.globalAjax(
			'/api/order/list',
			"get",
			data,
			function(e) {
				if (e.code == 0) {
					that.setData({
						orderList: e.orderList.list,
						pages: e.orderList.totalPage
					})
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			}, {
				token: wx.getStorageSync("token")
			})
	},
	Qpay: function(e) { //订单付款
		var i = e.target.dataset.index,
			that = this,
			$th = that.data.orderList[i]
		app.globalAjax(
			'/api/unifiedOrder',
			"post", {
				orderNumber: $th.orderNumber,
				totalAmount: 0.1
			},
			function(e) {
				if (e.code == 0) {
					if(!e.data){
						wx.showToast({
							title: '支付成功'
						})
						that.onLoad()
						return
					}
					app.pays(e.data, function(ee) {
						that.onLoad()
					}, function() {

					})
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			}, {
				token: wx.getStorageSync("token")
			})
	},
	gotoclass: function() { //去购物
		var pages = getCurrentPages(); // 获取页面栈
		var homePage = pages[0]; // 首页页面
		homePage.onLoad({
			cur: 'classify'
		})
		wx.navigateBack({
			delta: pages.length - 1
		})
	}
})
